package com.gmss.crawler.module.title.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.CommonEnum;
import com.gmss.crawler.module.title.domain.PersonTitle;
import com.gmss.crawler.module.title.service.IPersonTitleService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 人员头街表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-11-05
 */
@RestController
@RequestMapping("/personTitle")
public class PersonTitleController extends Base {


    @Resource
    private IPersonTitleService iPersonTitleService;


    /**
     * 通过id查询
     */
    @GetMapping("/getById/{id}")
    public String getById(@PathVariable(value = "id") Integer id) {
        return respJson(CommonEnum.SUCCESS, iPersonTitleService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public String save(@RequestBody PersonTitle personTitle) {
        iPersonTitleService.save(personTitle);
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/deleteById/{id}")
    public String delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            iPersonTitleService.removeById(Integer.parseInt(id));
        }
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public String updateById(@RequestBody PersonTitle personTitle) {
        iPersonTitleService.updateById(personTitle);
        return respJson(CommonEnum.SUCCESS);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public String list(@RequestBody PersonTitleReqVo personTitle) {
        final LambdaQueryWrapper<PersonTitle> lambda = new QueryWrapper<PersonTitle>().lambda();
        this.buildCondition(lambda, personTitle);
        return respJson(CommonEnum.SUCCESS, iPersonTitleService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public String page(@RequestBody PersonTitleReqVo pageParam) {
        final LambdaQueryWrapper<PersonTitle> lambda = new QueryWrapper<PersonTitle>().lambda();
        this.buildCondition(lambda, pageParam);
        final IPage<PersonTitle> page = iPersonTitleService.page(new Page<>(Constants.PAGE_NO, Constants.PAGE_SIZE), lambda);
        return respJson(CommonEnum.SUCCESS, page);
    }

    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<PersonTitle> lambda, PersonTitleReqVo param) {
        if (null != param.getId()) {
            lambda.eq(PersonTitle::getId, param.getId());
        }
        if (null != param.getPersonId()) {
            lambda.eq(PersonTitle::getPersonId, param.getPersonId());
        }
        if (null != param.getAcademicianTitle()) {
            lambda.eq(PersonTitle::getAcademicianTitle, param.getAcademicianTitle());
        }
        if (!StringUtils.isEmpty(param.getAcademicianTitleValue())) {
            lambda.eq(PersonTitle::getAcademicianTitleValue, param.getAcademicianTitleValue());
        }
        if (!CollectionUtils.isEmpty(param.getCreateTimeList())) {
            lambda.ge(PersonTitle::getCreateTime, param.getCreateTimeList().get(0));
            lambda.le(PersonTitle::getCreateTime, param.getCreateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            lambda.eq(PersonTitle::getCreateUser, param.getCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getUpdateTimeList())) {
            lambda.ge(PersonTitle::getUpdateTime, param.getUpdateTimeList().get(0));
            lambda.le(PersonTitle::getUpdateTime, param.getUpdateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUpdateUser())) {
            lambda.eq(PersonTitle::getUpdateUser, param.getUpdateUser());
        }
        lambda.orderBy(true, false, PersonTitle::getId);
    }


    /**
     * 请求model
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class PersonTitleReqVo extends PersonTitle {
        private List<String> createTimeList; // 创建时间起止
        private List<String> updateTimeList; // 修改时间起止
    }


}
